package com.topscomm.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.BasicSystemConst;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mis.IMisDao;
import com.topscomm.mis.IMisService;
import com.topscomm.mis.MisEntity;
import com.topscomm.mis.MisService;
import com.topscomm.mpm.pojo.*;
import com.topscomm.mpm.service.impl.MpmProjectPlanService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.ITapDao;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.TapService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Classname MarketBasicService
 * @Date 2022/3/9
 * @Created by jiangtao
 */
public class MarketBasicService <T extends MisEntity, TDao extends IMisDao> extends MisService<T, TDao>
		implements IMisService<T, TDao> {
	@Autowired
	private AmqpTemplate rabbitTemplate;
	private static ArrayList<String> mpmList = new ArrayList<>();
	static {
		mpmList.add(MpmProjectEntity.tableName);
		mpmList.add(MpmProjectPlanEntity.tableName);
		mpmList.add(MpmProjectAppointmentEntity.tableName);
		mpmList.add(MpmProjectAppointmentEntity.tableName);
		mpmList.add(MpmProjectTaskEntity.tableName);
		mpmList.add(MpmProjectSuperviseEntity.tableName);
		mpmList.add(MpmProjectProblemEntity.tableName);
		mpmList.add(MpmProjectStakeholderEntity.tableName);
		mpmList.add(MpmProjectRiskEntity.tableName);
		mpmList.add(MpmProjectDemandEntity.tableName);
		mpmList.add(MpmProjectReviewEntity.tableName);
	}
	@Override
	protected void afterInsert(BasicEntity entity) throws ServiceException {
		super.afterInsert(entity);
		JSONObject json;
		String tableName = dao.getTableName();
		Map<String,Object> result = new HashMap<>();
		result.put("operateType", "Insert");
		result.put("tablename",tableName);
		if(mpmList.indexOf(tableName)!=-1) {
			if(!MpmProjectEntity.tableName.equals(tableName)) {
				result.put("projectcode",entity.getValue("projectcode"));
			}
			result.put("sourceid",entity.getId());
			result.put("modifiedon",entity.getModifiedon());
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcMpmQueue",json.toJSONString());
		}
	}
	@Override
	protected void updateAfter(Map<String, Object> map) throws ServiceException {
		super.updateAfter(map);
		JSONObject json;
		String tableName = dao.getTableName();
		Map<String, Object> result = new HashMap<>();
		result.put("operateType", "Update");
		result.put("tablename", tableName);
		if(mpmList.indexOf(tableName)!=-1) {
			if(!MpmProjectEntity.tableName.equals(tableName)) {
				String projectcode = ConvertUtil.convertToString(map.get("projectcode"));
				if(StringUtil.isEmpty(projectcode)) {
					return;
				}
				result.put("projectcode",map.get("projectcode"));
			}
			result.put("sourceid",map.get("id"));
			result.put("modifiedon",ConvertUtil.convertToDate(map.get("modifiedon")));
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcMpmQueue",json.toJSONString());
		}
	}
}
